#include <windowsx.h>

#include "dxdiag_res.h"
#include "dxdiagcom.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(dxdiag);

LRESULT CALLBACK Network_DlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    HBRUSH g_hbrBackground = CreateSolidBrush(RGB(255,255,255));
    switch(uMsg) {
        case WM_INITDIALOG:
            return TRUE;
        case WM_CTLCOLORSTATIC:
            if (GetDlgCtrlID((HWND) lParam) == NETWORKTAB_NOTESEDIT) {
                return (LRESULT)g_hbrBackground;
            }
        default:
            break;
    }
    return FALSE;

}

BOOL NetworkInfo_GetInfo(DxDiagCOM* pcom, NetworkInfo* info)
{
    HRESULT hr;
    IDxDiagContainer *dplay;
    IDxDiagContainer *sps, *sp;
    IDxDiagContainer *apps, *app;
    BOOL b;

    UINT i;
    WCHAR query[BUF_SIZE];

    /* Get the DirectPlay container */
    if (FAILED(DxDiagCOM_GetContainer(pcom->root, (LPCWSTR)("DxDiag_DirectPlay"), &dplay)))
        return FALSE;

    /* Get the notes */
    DxDiagCOM_GetStringProperty(dplay, (LPCWSTR)(DxDiag_LoadString(NETWORK_SZNETWORKNOTESENGLISH)), &info->notes);

    /* Get the service provider container */
    DxDiagCOM_GetContainer(dplay, (LPCWSTR)("DxDiag_DirectPlaySPs"), &sps);

    /* Allocate space for each service provider */
    hr = IDxDiagContainer_GetNumberOfChildContainers(sps, &info->numSPs);
    if (FAILED(hr))
    {
        WINE_ERR("IDxContainer_GetNumberOfChildContainers failed\n");
        return FALSE;
    }

    info->sps = HeapAlloc(GetProcessHeap(), 0, sizeof(DPlaySPInfo) * info->numSPs);

    for (i = 0; i < info->numSPs; i++)
    {
        /* Get the name of the current SP */
        hr = IDxDiagContainer_EnumChildContainerNames(sps,i,query,BUF_SIZE);
        /* Get the SP container */
        IDxDiagContainer_GetChildContainer(sps,query, &sp);

        /* Get properties */
        DxDiagCOM_GetStringProperty(sp, (LPCWSTR)(DxDiag_LoadString(NETWORK_SZNAMEENGLISH)), &info->sps[i].name);
        DxDiagCOM_GetBoolProperty(sp, (LPCWSTR)(DxDiag_LoadString(NETWORK_BREGISTRYOK)), &b);
        info->sps[i].registry = BoolToChoice(b, (LPCWSTR)("OK"), (LPCWSTR)("Bad"));
        DxDiagCOM_GetStringProperty(sp, (LPCWSTR)(DxDiag_LoadString(NETWORK_SZFILE)), &info->sps[i].file);
        DxDiagCOM_GetStringProperty(sp, (LPCWSTR)(DxDiag_LoadString(NETWORK_SZVERSIONENGLISH)), &info->sps[i].version);
    }

    /* Get the adapter container */
    DxDiagCOM_GetContainer(dplay, (LPCWSTR)("DxDiag_DirectPlayApps"), &apps);

    /* Allocate space for each adapter */
    hr = IDxDiagContainer_GetNumberOfChildContainers(apps, &info->numApps);
    if (FAILED(hr))
    {
        WINE_ERR("IDxContainer_GetNumberOfChildContainers failed\n");
        return FALSE;
    }

    info->apps = HeapAlloc(GetProcessHeap(), 0, sizeof(DPlayAppInfo) * info->numApps);

    for (i = 0; i < info->numApps; i++)
    {
        /* Get the name of the current app */
        hr = IDxDiagContainer_EnumChildContainerNames(apps,i,query,BUF_SIZE);

        /* Get the app container */
        IDxDiagContainer_GetChildContainer(apps,query, &app);

        /* Get properties */
        DxDiagCOM_GetStringProperty(app, (LPCWSTR)(DxDiag_LoadString(NETWORK_SZNAME)), &info->apps[i].name);
        DxDiagCOM_GetBoolProperty(app, (LPCWSTR)("bRegistryOK"), &b);
        info->apps[i].registry = BoolToChoice(b, (LPCWSTR)("OK"), (LPCWSTR)("Bad"));
        DxDiagCOM_GetStringProperty(app, (LPCWSTR)(DxDiag_LoadString(NETWORK_SZEXEFILE)), &info->apps[i].file);
        DxDiagCOM_GetStringProperty(app, (LPCWSTR)(DxDiag_LoadString(NETWORK_SZEXEVERSIONENGLISH)), &info->apps[i].version);
        DxDiagCOM_GetStringProperty(app, (LPCWSTR)(DxDiag_LoadString(NETWORK_SZGUID)), &info->apps[i].guid);
    }

    return TRUE;
}

void Network_DisplayData()
{
    LVCOLUMN lvc;
    LVITEM lvi;
    UINT i;
    LPWSTR buf = NULL;
    HWND hWnd;

    HWND servList = GetDlgItem(hTabDisplay, NETWORKTAB_DSERVLIST);
    HWND appList = GetDlgItem(hTabDisplay, NETWORKTAB_DAPPSLIST);

    /* Disable the network buttons (not implemented) */
    hWnd = GetDlgItem(hTabDisplay, NETWORKTAB_VOICEOPTS);
    Button_Enable(hWnd, FALSE);
    hWnd = GetDlgItem(hTabDisplay, NETWORKTAB_TESTDPLAY);
    Button_Enable(hWnd, FALSE);

    /* Display the network notes */
    hWnd = GetDlgItem(hTabDisplay, NETWORKTAB_NOTESEDIT);
    SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)networkInfo.notes);

    /* Service provider list */
    /* Add columns */
    lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
    lvc.fmt = LVCFMT_LEFT;
    lvc.cx = 100;
    lvc.iSubItem = 0;

    lstrcpyW(buf, (LPCWSTR)("Name"));
    lvc.pszText = buf;
    ListView_InsertColumn(servList, 0, &lvc);

    lstrcpyW(buf, (LPCWSTR)("Registry"));
    lvc.pszText = buf;
    ListView_InsertColumn(servList, 1, &lvc);

    lstrcpyW(buf, (LPCWSTR)("File"));
    lvc.pszText = buf;
    ListView_InsertColumn(servList, 2, &lvc);

    lstrcpyW(buf, (LPCWSTR)("Version"));
    lvc.pszText = buf;
    ListView_InsertColumn(servList, 3, &lvc);

    /* Add items */
    lvi.mask = LVCF_TEXT;
    lvi.state = 0;
    lvi.stateMask = 0;

    for (i = 0; i < networkInfo.numSPs; i++)
    {
        lvi.iItem = i;
        lvi.iSubItem = 0;
        lvi.lParam = (LPARAM) &networkInfo.sps[i];
        lvi.pszText = LPSTR_TEXTCALLBACK;
        ListView_InsertItem(servList, &lvi);

          ListView_SetItemText(servList,i,0,networkInfo.sps[i].name);
          ListView_SetItemText(servList,i,1,networkInfo.sps[i].registry);
          ListView_SetItemText(servList,i,2,networkInfo.sps[i].file);
          ListView_SetItemText(servList,i,3,networkInfo.sps[i].version);
    }

    /* Registered Lobbyable DirectPlay Applications list */
    /* Add columns */
    lstrcpyW(buf, (LPCWSTR)("Name"));
    lvc.pszText = buf;
    ListView_InsertColumn(appList, 0, &lvc);
    lstrcpyW(buf, (LPCWSTR)("Registry"));
    lvc.pszText = buf;
    ListView_InsertColumn(appList, 1, &lvc);
    lstrcpyW(buf, (LPCWSTR)("File"));
    lvc.pszText = buf;
    ListView_InsertColumn(appList, 2, &lvc);
    lstrcpyW(buf, (LPCWSTR)("Version"));
    lvc.pszText = buf;
    ListView_InsertColumn(appList, 3, &lvc);
    lstrcpyW(buf, (LPCWSTR)("GUID"));
    lvc.pszText = buf;
    ListView_InsertColumn(appList, 4, &lvc);

    /* Add items */
    lvi.mask = LVCF_TEXT;
    lvi.state = 0;
    lvi.stateMask = 0;

    for (i = 0; i < networkInfo.numApps; i++)
    {
        lvi.iItem = i;
        lvi.iSubItem = 0;
        lvi.lParam = (LPARAM) &networkInfo.apps[i];
        lvi.pszText = LPSTR_TEXTCALLBACK;
        ListView_InsertItem(appList, &lvi);

        ListView_SetItemText(appList,i,0,networkInfo.apps[i].name);
        ListView_SetItemText(appList,i,1,networkInfo.apps[i].registry);
        ListView_SetItemText(appList,i,2,networkInfo.apps[i].file);
        ListView_SetItemText(appList,i,3,networkInfo.apps[i].version);
        ListView_SetItemText(appList,i,4,networkInfo.apps[i].guid);
    }
}

void Network_SaveText(FILE* pFile)
{
    /* TODO */
}

void Network_SaveXML(XML_Node* root)
{
    /* TODO */
}
